﻿package frEngine.animateControler.particleControler.animate
{
    import __AS3__.vec.*;
    import baseEngine.system.*;
    import flash.geom.*;
    import frEngine.animateControler.particleControler.*;
    import frEngine.math.*;

    public class UseRotateToVelocity extends Object implements IParticleAnimate
    {
        private const templeVector3d:Vector3D;
        private const _axis:Vector3D;
        private const _offset:Number = 1.5708;
        private const _matrix3d:Matrix3D;
        public static const instance:UseRotateToVelocity = new UseRotateToVelocity;

        public function UseRotateToVelocity()
        {
            this.templeVector3d = new Vector3D();
            this._axis = Vector3D.Z_AXIS;
            this._matrix3d = new Matrix3D();
            return;
        }// end function

        public function get objClass() : Class
        {
            return UseRotateToVelocity;
        }// end function

        public function canOpen(param1:ParticleParams) : Boolean
        {
            return param1.useRotateToVelocity;
        }// end function

        private function updateFaceCamera(param1:ParticleStatus) : void
        {
            var _loc_4:ParticleItem = null;
            var _loc_5:Vector3D = null;
            var _loc_6:Quaternion = null;
            var _loc_7:Vector3D = null;
            var _loc_8:Vector3D = null;
            var _loc_10:Vector3D = null;
            var _loc_11:Vector3D = null;
            var _loc_12:Number = NaN;
            var _loc_13:Number = NaN;
            var _loc_14:Number = NaN;
            var _loc_15:Number = NaN;
            var _loc_2:* = param1.particleList;
            var _loc_3:* = _loc_2.length;
            var _loc_9:int = 0;
            while (_loc_9 < _loc_3)
            {
                
                _loc_4 = _loc_2[_loc_9];
                if (!_loc_4.visible || !_loc_4.toUpdate)
                {
                }
                else
                {
                    _loc_10 = _loc_4.preResultPos;
                    _loc_5 = _loc_4.resultPos;
                    _loc_6 = _loc_4.selfRotate;
                    if (_loc_10.w == -1)
                    {
                        _loc_10.w = 0;
                        _loc_4.visible = 0;
                    }
                    else
                    {
                        this.templeVector3d.x = _loc_5.x - _loc_10.x;
                        this.templeVector3d.y = _loc_5.y - _loc_10.y;
                        this.templeVector3d.z = _loc_5.z - _loc_10.z;
                        this.templeVector3d.normalize();
                        _loc_4.startRotate.rotatePoint(this.templeVector3d, this.templeVector3d);
                        _loc_11 = Device3D.viewProjValue.deltaTransformVector(this.templeVector3d);
                        _loc_12 = Math.atan2(_loc_11.x, -_loc_11.y) + this._offset;
                        _loc_13 = Math.sin(_loc_12 / 2);
                        _loc_14 = Math.cos(_loc_12 / 2);
                        _loc_6.x = this._axis.x * _loc_13;
                        _loc_6.y = this._axis.y * _loc_13;
                        _loc_6.z = this._axis.z * _loc_13;
                        _loc_6.w = _loc_14;
                        _loc_15 = 1 / Math.sqrt(_loc_6.x * _loc_6.x + _loc_6.y * _loc_6.y + _loc_6.z * _loc_6.z + _loc_6.w * _loc_6.w);
                        _loc_6.x = _loc_6.x * _loc_15;
                        _loc_6.y = _loc_6.y * _loc_15;
                        _loc_6.z = _loc_6.z * _loc_15;
                        _loc_6.w = _loc_6.w * _loc_15;
                    }
                    _loc_10.x = _loc_5.x;
                    _loc_10.y = _loc_5.y;
                    _loc_10.z = _loc_5.z;
                }
                _loc_9++;
            }
            return;
        }// end function

        private function updateNotFaceCamera(param1:ParticleStatus) : void
        {
            var _loc_4:ParticleItem = null;
            var _loc_5:Vector3D = null;
            var _loc_6:Quaternion = null;
            var _loc_7:Vector3D = null;
            var _loc_8:Vector3D = null;
            var _loc_10:Vector3D = null;
            var _loc_11:Vector3D = null;
            var _loc_2:* = param1.particleList;
            var _loc_3:* = _loc_2.length;
            var _loc_9:int = 0;
            while (_loc_9 < _loc_3)
            {
                
                _loc_4 = _loc_2[_loc_9];
                if (!_loc_4.visible || !_loc_4.toUpdate)
                {
                }
                else
                {
                    _loc_10 = _loc_4.preResultPos;
                    _loc_5 = _loc_4.resultPos;
                    _loc_6 = _loc_4.selfRotate;
                    if (_loc_10.w == -1)
                    {
                        _loc_10.w = 0;
                        _loc_4.visible = 0;
                    }
                    else
                    {
                        this.templeVector3d.x = _loc_5.x - _loc_10.x;
                        this.templeVector3d.y = _loc_5.y - _loc_10.y;
                        this.templeVector3d.z = _loc_5.z - _loc_10.z;
                        this.templeVector3d.normalize();
                        if (this.templeVector3d.y > 0.99 || this.templeVector3d.y < -0.99)
                        {
                            _loc_6.x = 0;
                            _loc_6.y = 0;
                            _loc_6.z = 0;
                            _loc_6.w = 1;
                        }
                        else
                        {
                            _loc_8 = Vector3D.Z_AXIS;
                            _loc_7 = this.templeVector3d.crossProduct(_loc_8);
                            _loc_7.normalize();
                            _loc_8 = _loc_7.crossProduct(this.templeVector3d);
                            var _loc_12:int = 0;
                            this.templeVector3d.w = 0;
                            var _loc_12:* = _loc_12;
                            _loc_8.w = _loc_12;
                            _loc_7.w = _loc_12;
                            this._matrix3d.copyColumnFrom(0, _loc_7);
                            this._matrix3d.copyColumnFrom(1, this.templeVector3d);
                            this._matrix3d.copyColumnFrom(2, _loc_8);
                            _loc_11 = this._matrix3d.decompose(Orientation3D.QUATERNION)[1];
                            _loc_6.x = _loc_11.x;
                            _loc_6.y = _loc_11.y;
                            _loc_6.z = _loc_11.z;
                            _loc_6.w = _loc_11.w;
                        }
                    }
                    _loc_10.x = _loc_5.x;
                    _loc_10.y = _loc_5.y;
                    _loc_10.z = _loc_5.z;
                }
                _loc_9++;
            }
            return;
        }// end function

        public function update(param1:ParticleStatus) : void
        {
            if (param1.isFaceCamera)
            {
                this.updateFaceCamera(param1);
            }
            else
            {
                this.updateNotFaceCamera(param1);
            }
            return;
        }// end function

        public function init(param1:ParticleStatus, param2:ParticleParams) : void
        {
            if (param2.faceCamera)
            {
                param1.useSelfRotate = false;
            }
            else
            {
                param1.useSelfRotate = true;
            }
            return;
        }// end function

    }
}
